#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QFileDialog>
#include <QTextStream>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建横向布局
    QHBoxLayout * m_hboxLayout = new QHBoxLayout();
    QVBoxLayout * m_vbl_lst = new QVBoxLayout();
    QVBoxLayout * m_vbl_src = new QVBoxLayout();
    QVBoxLayout * m_vbl_ret = new QVBoxLayout();
    m_img_src = new XImage();
    m_img_ret = new XImage();
    m_list_view = new QListView(this);
    m_list_view->setFixedWidth(240);
    m_lvmodel = new QStandardItemModel(this);

    m_ln_min_number = new QLineEdit();
    m_ln_min_number->setFixedSize(240,40);
    m_ln_min_number->setValidator(new QIntValidator(0, 100, this));
    m_btn_ok = new QPushButton("过滤同一物点集");
    m_btn_ok->setFixedSize(240,20);
    //m_btn_ok->layout()->setMargin(10);

    //向布局中添加控件
    m_hboxLayout->addLayout(m_vbl_lst);
    m_hboxLayout->addLayout(m_vbl_src);
    m_hboxLayout->addLayout(m_vbl_ret);
    m_vbl_lst->setMargin(0);
    m_vbl_lst->addWidget(m_ln_min_number);
    m_vbl_lst->addWidget(m_btn_ok);
    m_vbl_lst->addWidget(m_list_view);
    m_vbl_src->addWidget(m_img_src);
    m_vbl_ret->addWidget(m_img_ret);


    QPalette palette;
    palette.setColor(QPalette::Background, QColor(192,192,192));
    m_img_src->setAutoFillBackground(true);
    m_img_src->setPalette(palette);
    m_img_ret->setAutoFillBackground(true);
    m_img_ret->setPalette(palette);

    //间隔
    m_hboxLayout->setSpacing(20);

    m_i_min_number = 0;

    //绑定布局
    this->centralWidget()->setLayout(m_hboxLayout);

    connect(m_list_view,SIGNAL(clicked(QModelIndex)),this,SLOT(LVItemClicked(QModelIndex)));

    connect(m_btn_ok, SIGNAL(clicked()), this, SLOT(Filtrate()));
}

void MainWindow::LVItemClicked(QModelIndex index)
{


    //qDebug() << index.data().toString();
    QStandardItem *pItem = (QStandardItem*)(m_lvmodel->item(index.row()));
    QVariant v = pItem->data();
    PCTSTHPSV pTSTHPSV= v.value <PCTSTHPSV>();
    qDebug() << "该视觉同一物点集中的边缘点数为：" << pTSTHPSV->ptsth->iEPTotal;

    m_img_ret->DrawWithTSTHPSV(*m_img_src, pTSTHPSV);

    //this->repaint();
    //this->m_img_src->repaint();
    this->m_img_ret->repaint();

}

void MainWindow::Filtrate()
{
   this->m_lvmodel->clear();
   this->m_i_min_number = m_ln_min_number->text().toInt();
    PCTSTHPSV pCurTSTHPSV = NULL;
    int i = 0;
    for(pCurTSTHPSV = g_tsthpsv_list;pCurTSTHPSV != NULL;pCurTSTHPSV = pCurTSTHPSV->next){
        if(pCurTSTHPSV->ptsth->iTotal > m_i_min_number) {
            QStandardItem *item = new QStandardItem();
            QString strText = "";
            strText.sprintf("%s%03d%s%03d","视觉同一物点集： [" , i , "], 总点数： ", pCurTSTHPSV->ptsth->iTotal);

            QVariant v;
            v.setValue(pCurTSTHPSV);
            item->setData(v);
            item->setText(strText);
            m_lvmodel->appendRow(item);
            i++;
        }
    }
    m_list_view->setModel(m_lvmodel);
}

MainWindow::~MainWindow()
{
    delete m_hboxLayout;
    delete m_vbl_lst;
    delete m_vbl_src;
    delete m_vbl_ret;
    delete m_img_src;
    delete m_img_ret;
    delete m_list_view;
    delete m_lvmodel;
    delete ui;
}

void MainWindow::on_ac_about_triggered()
{
    QMessageBox::information(this, "关于 EPTester：", "本程序是一个基于TheSCT思想的图像边缘点检测程序！");
}

void MainWindow::on_ac_open_triggered()
{
    QString path = QFileDialog::getOpenFileName(this,
                                                tr("打开文件："),
                                                ".",
                                                QString("位图文件(*.bmp)"));
    if(!path.isEmpty()) {

        m_lvmodel->clear();

        XBitmap *pbmp = new XBitmap();
        pbmp->load_img_file(path);

        m_img_src->load_bitmap(pbmp);
        m_img_ret->translate(*m_img_src);

        this->repaint();
        this->m_img_src->repaint();
        this->m_img_ret->repaint();

        if(g_tsthpsv_list == NULL){
            //QMessageBox::information(this, "警告：", "g_tsthpsv_list是空的！");
            ;
        }else{
            //QMessageBox::information(this, "高兴：", "g_tsthpsv_list里面有数据！");
            // 释放内存空间，待实现 。。。。。。
            g_tsthpsv_list = NULL;
        }

        CVisionMatterElementSet * pvmes = new CVisionMatterElementSet();
        pvmes->to_be_symbolic(pbmp);

        ::IterateVMES(*pvmes);

        this->m_lvmodel->clear();

        PCTSTHPSV pCurTSTHPSV = NULL;
        int i = 0;
        for(pCurTSTHPSV = g_tsthpsv_list;pCurTSTHPSV != NULL;pCurTSTHPSV = pCurTSTHPSV->next){
            if(pCurTSTHPSV->ptsth->iTotal > m_i_min_number) {
                QStandardItem *item = new QStandardItem();
                QString strText = "";
                strText.sprintf("%s%03d%s%03d","视觉同一物点集： [" , i , "], 总点数： ", pCurTSTHPSV->ptsth->iTotal);

                QVariant v;
                v.setValue(pCurTSTHPSV);
                item->setData(v);
                item->setText(strText);
                m_lvmodel->appendRow(item);
                i++;
            }
        }
        m_list_view->setModel(m_lvmodel);
    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton button;
    button = QMessageBox::question(this, QString("确认退出："),
                                   QString("您确定将要退出本程序?"),
                                   QMessageBox::Yes | QMessageBox::No);
    if (button == QMessageBox::No) {
          event->ignore();  //忽略退出信号，程序继续运行
    }
    else if (button == QMessageBox::Yes) {
          event->accept();  //接受退出信号，程序退出
    }
}

void MainWindow::on_ac_exit_triggered()
{
    QMessageBox::StandardButton button;
    button = QMessageBox::question(this, QString("确认退出："),
                                   QString("您确定将要退出本程序?"),
                                   QMessageBox::Yes | QMessageBox::No);
    if (button == QMessageBox::Yes) {
          QCoreApplication::exit();  //接受退出信号，程序退出
    }
}

void MainWindow::on_ac_test_triggered()
{
    //QString img_path = this->m_img_src->get_img_path();
    //QMessageBox::information(this, "当前加载图像路径是：",img_path);
}
